<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 搜索区域 -->
      <!-- 操作区域 -->
      <page-tools>
        <template #after>
          <el-button
            size="small"
            type="primary"
            @click="addPerm(1, '0')"
          >添加权限</el-button>
        </template>
      </page-tools>
      <!-- 表格区域 -->
      <el-table row-key="id" :data="list" border stripe>
        <!--
        <el-table-column prop="" label=""></el-table-column>
        <el-table-column prop="" label="">
        </el-table-column>
        <el-table-column prop="" label=""></el-table-column>
        <el-table-column prop="" label=""></el-table-column> -->
        <el-table-column
          v-for="(col, idx) in columns"
          :key="idx"
          :prop="col.prop"
          :align="col.align"
          :label="col.label"
        >
          <template slot-scope="{ row }">
            <el-tag v-if="col.prop === 'code'">{{ row[col.prop] }}</el-tag>
            <span v-else>{{ row[col.prop] }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="{ row }">
            <el-button type="text" @click="addPerm(2,row.id)">添加</el-button>
            <el-button
              type="text"
              @click="editRow(row.id)"
            >编辑</el-button>
            <el-button type="text" @click="delRow(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
    </div>
    <!-- 新增和修改弹框 -->
    <add-form ref="addForm" @updateList="initData" />
  </div>
</template>
<script>
import { getPermissionList, getPermissionDetail, delPermissionList } from '@/api/permission'
import { tranListToTreeData } from '@/utils'
import addForm from './components/addForm.vue'
export default {
  name: 'Permission',
  components: {
    addForm
  },
  data() {
    return {
      // 表格数据
      list: [],
      // 列的数据
      columns: [
        { label: '名称', prop: 'name', align: 'center' },
        { label: '标识', prop: 'code', align: 'center' },
        { label: '描述', prop: 'description', align: 'center' }
      ]
    }
  },
  created() {
    this.initData()
  },
  methods: {
    // 获取表格的数据
    async initData() {
      const res = await getPermissionList()
      // console.log(res) // 必须检查network，看console 包装请求成功了数据
      this.list = tranListToTreeData(res, '0') // 看vue调试器
    },
    // 添加权限
    addPerm(type, pid) {
      this.$refs.addForm.formData.type = type
      this.$refs.addForm.formData.pid = pid
      this.$refs.addForm.dialogShow = true
    },
    // 编辑权限
    async editRow(id) {
      // 查询单个数据
      const res = await getPermissionDetail(id)
      this.$refs.addForm.formData = res
      this.$refs.addForm.dialogShow = true
    },
    // 删除权限,删除一般都会传递id
    async  delRow(id) {
      await this.$confirm('删除后无法恢复,确定要删除嘛？')// 二次确认
      await delPermissionList(id) // 发送删除请求
      this.$message.success('删除数据成功')
      this.initData() // 更新列表
    }
  }
}
</script>
<style></style>
